package com.wgchao.identityrightsmanagement.domain.resource.model;

import com.wgchao.identityrightsmanagement.domain.IdentifiedDomainObject;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectDisabledException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName: MenuResources
 * @author: wgchao
 * @createTime: 2020/6/9 3:49 PM
 * @Description: 菜单资源
 */
@Getter
@AllArgsConstructor
public class MenuResources extends IdentifiedDomainObject {

    //标识
    private String menuMark;

    //项目标识
    private String proMark;

    private String menuName;

    //上级菜单标识
    private String superiorMark;

    //描述/备注
    private String description;


    public MenuResources(Long id, String menuMark, String proMark, String menuName, String superiorMark,
                         String description, Long version) {
        setId(id);
        this.menuMark = menuMark;
        this.proMark = proMark;
        this.menuName = menuName;
        this.superiorMark = superiorMark;
        this.description = description;
        setVersion(version);
    }

    /**
     * 添加菜单资源
     */
    public static MenuResources addmenu(ProjectResources projectResources,  MenuResourceCreator creator){
        if(!projectResources.normal()){
            throw new ObjectDisabledException("项目资源不可用");
        }
        return new MenuResources(creator.getMenuMark(), projectResources.getProMark(),
                creator.getMenuName(), creator.getSuperiorMark(),
                creator.getDescription());
    }

    /**
     * 修改菜单信息
     * @param projectResources
     * @param updator
     */
    public void modifyMenuInfo(ProjectResources projectResources, MenuResourcesUpdator updator){
        if(!projectResources.normal()){
            throw new ObjectDisabledException("项目资源不可用");
        }
        if(updator.getMenuName() != null){
            this.menuName = updator.getMenuName().orElse(null);
        }
        if(updator.getDescription() != null){
            this.description = updator.getDescription().orElse(null);
        }
    }

    /**
     * 添加菜单功能
     * @param creator
     * @return
     */
    public FunctionResources addFunc(FuncResourcesCreator creator){
        return new FunctionResources(this.menuMark, creator.getFuncMark(), creator.getFuncName());
    }


    /**
     * 修改菜单功能名称
     * @param functionResources
     * @param funcResourcesUpdator
     * @return
     */
    public FunctionResources modifyFuncName(FunctionResources functionResources, FuncResourcesUpdator funcResourcesUpdator){
        functionResources.modifyFuncName(funcResourcesUpdator);
        return functionResources;
    }


}
